<?php
declare (strict_types = 1);

namespace app\admin\model;

use app\common\model\Admin;
use think\Model;

/**
 * @mixin think\Model
 */
class User extends Admin
{
    public function fileIdImage()
    {
        return $this->belongsTo('file');
    }
    public function usergroup()
    {
        return $this->hasMany('UserGroup');
    }
    //新增前
    public static function onBeforeInsert($data)
    {
        if (isset($data['password']) && $data['password'] === '') unset($data['password']);
        if (isset($data['password'])) $data['password'] = jm($data['password']);
        parent::onBeforeInsert($data); // TODO: Change the autogenerated stub
    }
    //新增后
    public static function onAfterInsert($data)
    {
        if(isset($data['file_id']) && $data['file_id'] > 0){
            $file_model = app('app\admin\model\File');
            $file_model::update(['id'=>$data['file_id'],'pid'=>$data->id]);
        }
        parent::onAfterInsert($data); // TODO: Change the autogenerated stub
    }
    //更新前
    public static function onBeforeUpdate($data)
    {
        if (isset($data['password']) && $data['password'] === '') unset($data['password']);
        if (isset($data['password'])) $data['password'] = jm($data['password']);
        parent::onBeforeUpdate($data); // TODO: Change the autogenerated stub
    }
}
